package com.salelogistics.basic.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.salelogistics.basic.domain.BasBuildUnit;
import com.salelogistics.basic.domain.vo.BasBuildUnitVo;
import com.salelogistics.basic.domain.vo.BasContractVo;
import com.salelogistics.basic.utils.CheckParameterUtil;
import com.salelogistics.common.core.utils.StringUtils;
import com.salelogistics.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.basic.domain.BasContract;
import com.salelogistics.basic.service.IBasContractService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 合同Controller
 *
 * @author ruoyi
 * @date 2023-07-10
 */
@RestController
@RequestMapping("contract")
public class BasContractController extends BaseController {
    //日志打印类
    private static final Logger log = LoggerFactory.getLogger(BasSupplierController.class);

    //数据校验工具
    CheckParameterUtil checkParameterUtil = new CheckParameterUtil();

    @Autowired
    private IBasContractService basContractService;

    /**
     * 查询合同列表
     */
    @RequiresPermissions("basic:contract:list")
    @GetMapping("/list")
    public TableDataInfo list(BasContract basContract) {
        startPage();
        List<BasContract> list = basContractService.selectBasContractList(basContract);
        return getDataTable(list);
    }

    /**
     * 查询合同列表Vo
     */
    @RequiresPermissions("basic:contract:listVo")
    @GetMapping("/listVo")
    public TableDataInfo listVo(BasContract basContract) {
        List<BasContractVo> listVo=new ArrayList<>();
        startPage();
        List<BasContract> list = basContractService.selectBasContractList(basContract);
        for (int i = 0; i < list.size(); i++) {
            BasContractVo basContractVo = BeanUtil.toBean(list.get(i), BasContractVo.class);
            listVo.add(basContractVo);
        }
        return getDataTable(listVo);
    }
    /**
     * 导出合同列表
     */
    @RequiresPermissions("basic:contract:export")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasContract basContract) {
        List<BasContract> list = basContractService.selectBasContractList(basContract);
        ExcelUtil<BasContract> util = new ExcelUtil<BasContract>(BasContract.class);
        util.exportExcel(response, list, "合同数据");
    }

    /**
     * 获取合同详细信息
     */
    @RequiresPermissions("basic:contract:query")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId) {
        return success(basContractService.selectBasContractByContractId(contractId));
    }



    /**
     * 新增合同
     */
    @RequiresPermissions("basic:contract:add")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasContract basContract) {
        if (checkParameterUtil.checkParameter(basContract, "basContract")) {
            basContract.setCreateBy(SecurityUtils.getUsername());
            basContract.setEditedBy(SecurityUtils.getUsername());
            return toAjax(basContractService.insertBasContract(basContract));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 修改合同
     */
    @RequiresPermissions("basic:contract:edit")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasContract basContract) {
        if (basContract == null) {
            return error("参数有误");
        }
        if (StringUtils.isNotEmpty(basContract.getCommunityName())
                && StringUtils.isNotEmpty(basContract.getContractName())
                && StringUtils.isNotEmpty(basContract.getEndTime())
                && StringUtils.isNotEmpty(basContract.getContractType())
        ) {
            // 创建一个Date对象，表示当前时间
            Date date = new Date();
            // 创建一个SimpleDateFormat对象，指定时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
            String currentTime = sdf.format(date);
            basContract.setEditedBy(SecurityUtils.getUsername());
            basContract.setEditedTime(currentTime);
            return toAjax(basContractService.updateBasContract(basContract));
        } else {
            return error("必填字段不规范");
        }

    }

    /**
     * 删除合同
     */
    @RequiresPermissions("basic:contract:remove")
    @Log(title = "合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds) {
        return toAjax(basContractService.deleteBasContractByContractIds(contractIds));
    }
}
